import * as THREE from 'three';
import { call } from 'three/tsl';
const v2_down = new THREE.Vector2();
const v2_up = new THREE.Vector2();
let intersects;
function setupEventListeners(dom, eventType, select, camera, callback) {

    function getMousePosition(dom, x, y) {
        const rect = dom.getBoundingClientRect();
        return [(x - rect.left) / rect.width, (y - rect.top) / rect.height];

    }

    function onMouseDown(event) {
        const array = getMousePosition(dom, event.clientX, event.clientY)
        v2_down.fromArray(array);

        dom.addEventListener('mouseup', onMouseUp);
    }

    function onMouseLeave(event) {

        const array = getMousePosition(dom, event.clientX, event.clientY)
        v2_up.fromArray(array);

        checkIntersection(event);

    }
    function onDoubleClick(event) {

        getMousePosition(dom, event.clientX, event.clientY)
        checkIntersection(event);

    }
    function onContextMenu(event) {
        getMousePosition(dom, event.clientX, event.clientY)

        // 判断距离 避免 鼠标松开选择其他模型
        if (v2_down.distanceTo(v2_up) === 0) {
            checkIntersection(event);
        }
        // checkIntersection(event);

    }

    function onMouseUp(event) {


        const array = getMousePosition(dom, event.clientX, event.clientY)
        v2_up.fromArray(array);

        // 判断距离 避免 鼠标松开选择其他模型
        if (v2_down.distanceTo(v2_up) === 0) {
            checkIntersection(event);
        }

        window.removeEventListener('mouseup', onMouseUp);


    }

    function onMouseMove(event) {

        const array = getMousePosition(dom, event.clientX, event.clientY)
        v2_up.fromArray(array);

        checkIntersection(event);

    }

    function addEventListener(dom, eventType, handler) {
        window.addEventListener(eventType, handler);
    }
    function checkIntersection(event) {

        intersects = select.getPointerIntersects(v2_up, camera);

        callback(intersects);

    }
    function removeEventListener(dom, eventType, handler) {
        window.removeEventListener(eventType, handler);
    }


    // 移除所有可能的事件监听
    removeEventListener(dom, 'mousedown', onMouseDown);
    removeEventListener(dom, 'mousemove', onMouseMove);
    removeEventListener(dom, 'dblclick', onDoubleClick);
    removeEventListener(dom, 'contextmenu', onContextMenu);
    removeEventListener(dom, 'mouseleave', onMouseLeave);

    // 根据事件类型添加相应的事件监听
    switch (eventType) {
        case 'click':
            
            addEventListener(dom, 'mousedown', onMouseDown);
            break;
        // case 'dblclick':
        //     addEventListener(dom, 'dblclick', onDoubleClick);
        //     break;
        // case 'contextmenu':
        //     addEventListener(dom, 'contextmenu', onContextMenu);
        //     break;
        case 'move':
            addEventListener(dom, 'mousemove', onMouseMove);
            break;
        case 'mouseleave':
            addEventListener(dom, 'mouseleave', onMouseLeave);
            break;
        default:
            console.warn(`Unhandled event type: ${eventType}`);
            break;
    }
}

export { setupEventListeners }